import { Plugin } from 'graphql-yoga'
 
export function useAuth(): Plugin {
 return {
  onRequest({ request, fetchAPI, endResponse }) {
    if (!request.headers.get('authorization')) {
      endResponse(
        new fetchAPI.Response(
          null,
          {
            status: 401,
            headers: {
              'Content-Type': 'application/json'
            }
          }
        )
      )
    }
  },
  onResponse({ request, response }) {
    response.headers.set('X-GraphQL-Server', 'Yoga')
  }
 }
}